package tenumn.energytech.registrys.recipe;

import com.google.gson.annotations.Expose;
import com.mojang.datafixers.util.Pair;
import io.github.uicdb.liba.api.register.LazyRecipe;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.Level;
import net.minecraftforge.items.wrapper.RecipeWrapper;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class OneToTwoRecipe extends LazyRecipe {
    @Expose
    public final Ingredient input;
    @Expose
    public final ItemStack outFirst;
    @Expose
    public final ItemStack outSecond;
    @Expose
    public final double secondChance;

    @Override
    public boolean canCraftInDimensions(int pWidth, int pHeight) {
        return true;
    }

    public OneToTwoRecipe(Ingredient input, ItemStack outFirst, ItemStack outSecond, double secondChance) {
        super();
        this.input = input;
        this.outFirst = outFirst;
        this.outSecond = outSecond;
        this.secondChance = secondChance;
    }

    @Override
    public boolean matches(RecipeWrapper pContainer, Level pLevel) {
        var conItem=pContainer.getItem(0);
        var out1=pContainer.getItem(1);
        var out2=pContainer.getItem(2);
        boolean checkOut1=out1.isEmpty()||(out1.sameItem(outFirst)&&out1.getCount()+outFirst.getCount()<outFirst.getMaxStackSize());
        boolean checkOut2=out2.isEmpty()||(out2.sameItem(outSecond)&&out2.getCount()+outSecond.getCount()<outSecond.getMaxStackSize());
        return input.test(conItem)&&checkOut1&&checkOut2;
    }

    @Override
    public ItemStack assemble(RecipeWrapper pContainer) {
        throw new UnsupportedOperationException("Recipe may have two Results.You should call assembleRealResult instead.");
    }

    @Override
    public ItemStack getResultItem() {
        return outFirst;
    }
    public Pair<ItemStack,ItemStack> assembleRealResult(RecipeWrapper pContainer,boolean multiple){
        var conInput=pContainer.getItem(0);
        for (ItemStack itemStack : input.getItems()) {
            if(itemStack.sameItem(conInput)) {
                int stackSize = conInput.getCount() % itemStack.getCount();
                var realOutFirst=outFirst.copy();
                if(multiple)
                    realOutFirst.setCount(realOutFirst.getCount()*stackSize);
                var realOutSecond=outSecond.copy();
                if(multiple)
                    realOutSecond.setCount(realOutSecond.getCount()*stackSize);
                return new Pair<>(realOutFirst,Math.random()<=secondChance?realOutSecond:ItemStack.EMPTY);
            }
        }
        return new Pair<>(ItemStack.EMPTY,ItemStack.EMPTY);
    }

    @Override
    public NonNullList<Ingredient> getIngredients() {
        return NonNullList.of(input);
    }

    @Override
    public NonNullList<ItemStack> getRemainingItems(RecipeWrapper pContainer) {
        return super.getRemainingItems(pContainer);
    }

}
